package dao.impl;

import dao.ManageDao;
import db.DbConnection;
import pojo.Book;
import pojo.Manage;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class ManageDaoImpl implements ManageDao {
    private Connection conn = null;
    private PreparedStatement ps = null;
    private ResultSet rs = null;

    @Override
    public List<Manage> searchAll() {
        conn = DbConnection.getConnection();
        List<Manage> manageList = new ArrayList<Manage>();
        try {
            String sql = "select * from manage ";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                String userId = rs.getString(1);
                String userName = rs.getString(2);
                String bookId = rs.getString(3);
                String bookName = rs.getString(4);
                String lendTime = rs.getString(5);
                String returnTime = rs.getString(6);
                Manage manage = new Manage(userId, userName, bookId, bookName, lendTime, returnTime);
                manageList.add(manage);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(ps, rs, conn);
        }
        return manageList;
    }

    @Override
    public void add(Manage manage) {
        Connection conn = DbConnection.getConnection();
        Timestamp timestamp0 = new Timestamp(System.currentTimeMillis());
        Calendar c = Calendar.getInstance();
        c.setTime(timestamp0);
//        c.add(Calendar.DATE, 1);  //  加一  天
        c.add(Calendar.MONTH, 1); //  加一个月
        //c.add(Calendar.YEAR,1);   //  加一  年
        Timestamp time1 = new Timestamp(c.getTimeInMillis());
        try {
            String sql = "insert into manage values(?,?,?,?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1, manage.getUserId());
            ps.setString(2, manage.getUserName());
            ps.setString(3, manage.getBookId());
            ps.setString(4, manage.getBookName());
            ps.setTimestamp(5, timestamp0);
            ps.setString(6, String.valueOf(time1));

            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(ps, rs, conn);
        }
    }

    @Override
    public void update(Manage manage) {
        Connection conn = DbConnection.getConnection();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Timestamp ts = Timestamp.valueOf(manage.getReturnTime());
        Timestamp lendTime = Timestamp.valueOf(manage.getLendTime());
        try {
            String sql = "update manage set userName=?,bookName=?,lendTime=?,returnTime=? where bookId=? and userId=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, manage.getUserName());
            ps.setString(2, manage.getBookName());
            ps.setTimestamp(3, lendTime);
            ps.setTimestamp(4, ts);
            ps.setString(5, manage.getBookId());
            ps.setString(6, manage.getUserId());
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(ps, rs, conn);
        }
    }


    @Override
    public void delete(String bookId) {
        Connection conn = DbConnection.getConnection();
        try {
            String sql = "delete from manage where bookId=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, bookId);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(ps, rs, conn);
        }
    }

    @Override
    public void deleteLend(String lendTime) {
        Connection conn = DbConnection.getConnection();
        Timestamp lendTime1 = Timestamp.valueOf(lendTime);
        try {
            String sql = "delete from manage where lendTime=?";
            ps = conn.prepareStatement(sql);
            ps.setTimestamp(1, lendTime1);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(ps, rs, conn);
        }
    }

    @Override
    public Manage searchId(String id) {
        conn = DbConnection.getConnection();
        Manage manage = null;
        try {
            String sql = "select * from manage where bookId=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, id);
            rs = ps.executeQuery();
            while (rs.next()) {
                String bookId = rs.getString(1);
                String bookName = rs.getString(2);
                String userId = rs.getString(3);
                String userName = rs.getString(4);
                String lendTime = rs.getString(5);
                String returnTime = rs.getString(6);
                manage = new Manage(bookId, bookName, userId, userName, lendTime, returnTime);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(ps, rs, conn);
        }
        return manage;
    }

    @Override
    public Manage searchLend(String lend) {
        conn = DbConnection.getConnection();
        Manage manage = null;
        try {
            String sql = "select * from manage where lendTime=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, lend);
            rs = ps.executeQuery();
            while (rs.next()) {
                String bookId = rs.getString(1);
                String bookName = rs.getString(2);
                String userId = rs.getString(3);
                String userName = rs.getString(4);
                String lendTime = rs.getString(5);
                String returnTime = rs.getString(6);
                manage = new Manage(bookId, bookName, userId, userName, lendTime, returnTime);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbConnection.close(ps, rs, conn);
        }
        return manage;
    }
}